package com.example.mattrigle;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.string;
import android.os.Environment;
import android.util.Log;

public class SaveData {

	
	//=============เมื่อกดปุ่มเลือกมุม
	public static String PlayerName="";
	public static String DateTime_StartTask="";// [yyyy-MM-dd HH:mm:ss]
	public static String Angle="";//มุมโจทย์ [30 45 60]
	public static String TaskNO=""; // ตอบครั้งที่ [1-4]
	public static String Origin_position = ""; // ค่ามุมเริ่มต้น
	
	
	
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ 1 ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> ID_SinCos=new ArrayList<String>();  //  [1,2]
	public static ArrayList<String> Time_ClickSinCos=new ArrayList<String>();  //  [HH:mm:ss]
		
		
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ 2 ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> DragXY_Graph=new ArrayList<String>();  //  [x,y]
	public static ArrayList<String> StartTime_Touch_Graph=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> StartPosition_XY_Graph=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> StartTime_Drag_Graph=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> EndPosition_XY_Graph=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> EndTime_Drag_Graph=new ArrayList<String>();  //  [HH:mm:ss]


	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❸ ให้เก็บข้อมูลดังต่อไปนี้

	public static ArrayList<String> DragXY_SeekBar=new ArrayList<String>();  //  [x,y]
	public static ArrayList<String> StartTime_Touch_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> StartPosition_XY_SeekBar=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> StartTime_Drag_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> EndPosition_XY_SeekBar=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> EndTime_Drag_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]


	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❹ ให้เก็บข้อมูลดังต่อไปนี้ี้

	public static ArrayList<String> Click_SinCos=new ArrayList<String>();  //  [BtnSin,BtnCos]
	public static ArrayList<String> StartTime_Click_BtnSinCos=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Value_XY_BtnSinCos=new ArrayList<String>();  //  [(x,y)]
	

	


    //============เมื่อผู้ใช้ interact  กับส่วนที่ ❺ ให้เก็บข้อมูลดังต่อไปนี้

	public static ArrayList<String> Time_Sureness=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Value_Sureness=new ArrayList<String>();  //  [1-4]

	
   //============เมื่อผู้ใช้ interact  กับส่วนที่ ❻ ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> Time_ScreenShot=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> NO_ScreenShot=new ArrayList<String>();  //  [1-3]

	
	
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❼ ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> Time_OkBtn=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Angle_Player=new ArrayList<String>();  //  [0-90]	
	public static ArrayList<String> Angle_Discrepancy=new ArrayList<String>();  //  [30-60]

	public static ArrayList<String> CosMinus_click = new ArrayList<String>();
	public static ArrayList<String> CosMinus_time = new ArrayList<String>();
	public static ArrayList<String> CosMinus_value = new ArrayList<String>();
	
	public static ArrayList<String> CosPlus_click = new ArrayList<String>();
	public static ArrayList<String> CosPlus_time = new ArrayList<String>();
	public static ArrayList<String> CosPlus_value = new ArrayList<String>();
	
	public static ArrayList<String> SinMinus_click = new ArrayList<String>();
	public static ArrayList<String> SinMinus_time = new ArrayList<String>();
	public static ArrayList<String> SinMinus_value = new ArrayList<String>();
	
	public static ArrayList<String> SinPlus_click = new ArrayList<String>();
	public static ArrayList<String> SinPlus_time = new ArrayList<String>();
	public static ArrayList<String> SinPlus_value = new ArrayList<String>();
	
	public static ArrayList<String> SinSpin_click = new ArrayList<String>();
	public static ArrayList<String> SinSpin_time = new ArrayList<String>();
	public static ArrayList<String> SinSpin_value = new ArrayList<String>();
	
	public static ArrayList<String> CosSpin_click = new ArrayList<String>();
	public static ArrayList<String> CosSpin_time = new ArrayList<String>();
	public static ArrayList<String> CosSpin_value = new ArrayList<String>();
	
	// ============================ json Format ==================================== //
	public static String jsonFormat() throws JSONException{
		String json  = "";
		
		JSONObject id_sincos = new JSONObject();
		JSONArray js_arr_sincos = new JSONArray();
		int size = ID_SinCos.size();
		for(int i = 0; i < size; i++){
			JSONObject obj = new JSONObject();
				obj.put("Angle", ID_SinCos.get(i));
				obj.put("Time", Time_ClickSinCos.get(i));
				js_arr_sincos.put(obj);
		}
		id_sincos.put("Angle_H_V", js_arr_sincos);
				
		JSONObject id_dragxy_graph = new JSONObject();
		JSONArray js_arr_dragxp_graph = new JSONArray();
		int size1 = DragXY_Graph.size();
		for(int i = 0; i < size1; i++){
			JSONObject obj = new JSONObject();
				obj.put("Drag", DragXY_Graph.get(i));
				obj.put("Touch_Start", StartTime_Touch_Graph.get(i));
				obj.put("Position_Start", StartPosition_XY_Graph.get(i));
				obj.put("Position_End", EndPosition_XY_Graph.get(i));
				obj.put("Drag_Start", StartTime_Drag_Graph.get(i));				
				obj.put("Drag_End", EndTime_Drag_Graph.get(i));
			js_arr_dragxp_graph.put(obj);
		}
		id_dragxy_graph.put("Graph", js_arr_dragxp_graph);
		
		
		JSONObject id_dragxy_seekbar = new JSONObject();
		JSONArray js_arr_dragxp_seekbar = new JSONArray();
		int size2 = DragXY_SeekBar.size();
		for(int i = 0; i < size2; i++){
			JSONObject obj = new JSONObject();
				obj.put("Drag", DragXY_SeekBar.get(i));
				obj.put("Touch_Start", StartTime_Touch_SeekBar.get(i));
				obj.put("Position_Start", StartPosition_XY_SeekBar.get(i));
				obj.put("Position_End", StartTime_Drag_SeekBar.get(i));
				obj.put("Drag_Start", EndPosition_XY_SeekBar.get(i));				
				obj.put("Drag_End", EndTime_Drag_SeekBar.get(i));
				js_arr_dragxp_seekbar.put(obj);
		}
		id_dragxy_seekbar.put("SeekBar", js_arr_dragxp_seekbar);
		
		
		JSONObject click_sincos = new JSONObject();
		JSONArray js_click_sincos = new JSONArray();
		int size3 = Click_SinCos.size();
		for(int i = 0; i < size3; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", Click_SinCos.get(i));
				obj.put("Time", StartTime_Click_BtnSinCos.get(i));
				obj.put("Value", Value_XY_BtnSinCos.get(i));
				js_click_sincos.put(obj);
		}
		click_sincos.put("Button_Next_Angle", js_click_sincos);
		
		
		JSONObject sureness = new JSONObject();
		JSONArray js_sureness = new JSONArray();
		int size4 = Value_Sureness.size();
		for(int i = 0; i < size4; i++){
			JSONObject obj = new JSONObject();
				obj.put("Value", Value_Sureness.get(i));
				obj.put("Time", Time_Sureness.get(i));
				js_sureness.put(obj);
		}
		sureness.put("Sureness", js_sureness);
		
		JSONObject screenshot = new JSONObject();
		JSONArray js_screenshot = new JSONArray();
		int size5 = NO_ScreenShot.size();
		for(int i = 0; i < size5; i++){
			JSONObject obj = new JSONObject();
				obj.put("Number", NO_ScreenShot.get(i));
				obj.put("Time", Time_ScreenShot.get(i));
				js_screenshot.put(obj);
		}
		screenshot.put("ScreenShot", js_screenshot);
		
		
		JSONObject btn_ok = new JSONObject();
		JSONArray js_btn_ok = new JSONArray();
		int size6 = Angle_Player.size();
		for(int i = 0; i < size6; i++){
			JSONObject obj = new JSONObject();
				obj.put("Angle_Answer", Angle_Player.get(i));
				obj.put("Angle_Discrepancy", Angle_Discrepancy.get(i));
				obj.put("Time", Time_OkBtn.get(i));				
				js_btn_ok.put(obj);
		}
		btn_ok.put("Button_OK_Click", js_btn_ok);
		
		JSONObject Cosminus = new JSONObject();
		JSONArray js_Cosminus = new JSONArray();
		int size7 = CosMinus_click.size();
		for(int i = 0; i < size7; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", CosMinus_click.get(i));
				obj.put("Value", CosMinus_value.get(i));
				obj.put("Time", CosMinus_time.get(i));
				js_Cosminus.put(obj);
		}
		Cosminus.put("CosMinus", js_Cosminus);
		
		JSONObject Sinminus = new JSONObject();
		JSONArray js_sinminus = new JSONArray();
		int size8 = SinMinus_click.size();
		for(int i = 0; i < size8; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", SinMinus_click.get(i));
				obj.put("Value", SinMinus_value.get(i));
				obj.put("Time", SinMinus_time.get(i));
				js_sinminus.put(obj);
		}
		Sinminus.put("SinMinus", js_sinminus);
		

		JSONObject Cosplus = new JSONObject();
		JSONArray js_Cosplus = new JSONArray();
		int size9 = CosPlus_click.size();
		for(int i = 0; i < size9; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", CosPlus_click.get(i));
				obj.put("Value", CosPlus_value.get(i));
				obj.put("Time", CosPlus_time.get(i));
				js_Cosplus.put(obj);
		}
		Cosplus.put("CosPlus", js_Cosplus);
		
		JSONObject Sinplus = new JSONObject();
		JSONArray js_Sinplus = new JSONArray();
		int size10 = SinPlus_click.size();
		for(int i = 0; i < size10; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", SinPlus_click.get(i));
				obj.put("Value", SinPlus_value.get(i));
				obj.put("Time", SinPlus_time.get(i));
				js_Sinplus.put(obj);
		}
		Sinplus.put("SinPlus", js_Sinplus);
		
		JSONObject Dialog_Sin = new JSONObject();
		JSONArray js_Dialog_Sin = new JSONArray();
		int size11 = SinSpin_click.size();
		for(int i = 0; i < size11; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", SinSpin_click.get(i));
				obj.put("Value", SinSpin_value.get(i));
				obj.put("Time", SinSpin_time.get(i));
				js_Dialog_Sin.put(obj);
		}
		Dialog_Sin.put("Dialog_Sin", js_Dialog_Sin);
		
		JSONObject Dialog_Cos = new JSONObject();
		JSONArray js_Dialog_Cos = new JSONArray();
		int size12 = CosSpin_click.size();
		for(int i = 0; i < size12; i++){
			JSONObject obj = new JSONObject();
				obj.put("Click", CosSpin_click.get(i));
				obj.put("Value", CosSpin_value.get(i));
				obj.put("Time", CosSpin_time.get(i));
				js_Dialog_Cos.put(obj);
		}
		Dialog_Cos.put("Dialog_Cos", js_Dialog_Cos);
		
		
		JSONArray js_main = new JSONArray();
			js_main.put(id_sincos);
			js_main.put(id_dragxy_graph);
			js_main.put(id_dragxy_seekbar);
			js_main.put(click_sincos);
			js_main.put(Sinminus);
			js_main.put(Sinplus);
			js_main.put(Cosminus);
			js_main.put(Cosplus);
			js_main.put(Dialog_Sin);
			js_main.put(Dialog_Cos);
			js_main.put(sureness);
			js_main.put(screenshot);
			js_main.put(btn_ok);
		
		JSONObject obj_main = new JSONObject();
		
		obj_main.put("Play_Date", DateTime_StartTask);
		obj_main.put("Angle Select", Angle);
		obj_main.put("Game Round", TaskNO);
		obj_main.put("PlayGame", js_main);
		obj_main.put("Origin Position", Origin_position);
		obj_main.put("PlayerName", PlayerName);
			
			
			
			
			
			
			
			
		
		json = obj_main.toString(4);			
		return json;
	}
	
	
	
	// ============================ Set Format ===================================== //
	public static String setFormat(){
		String data = "Player Name = " + PlayerName +"\nDate = "+DateTime_StartTask+"\nAngle = "+Angle+"\nTaskNO = "+TaskNO+"\n";
		data = data + "Click Button Sin-Cos : \n\t";
		// +-- btn sincos --+ //
		int size = ID_SinCos.size();
		for(int i = 0; i < size; i++){
			data = data + "Click Button = [" + ID_SinCos.get(i) + " , " + Time_ClickSinCos.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- Graph --+ //
		data = data + "Graph : \n\tDragXY = (\n\t\t";
		int size2 = DragXY_Graph.size();
		for(int i = 0; i < size2; i++){
			data = data + "[" + DragXY_Graph.get(i) + "],";
		}
		data = data + "\n\tStartTime Touch = (\n\t\t";
		for(int i = 0; i < StartTime_Touch_Graph.size(); i++){
			data = data + "[" + StartTime_Touch_Graph.get(i) + "],";
		}
		data = data + ")\n\tStartPosition XY = (\n\t\t";
		for(int i = 0; i < StartPosition_XY_Graph.size(); i++){
			data = data + "[" + StartPosition_XY_Graph.get(i) + "],";
		}
		data = data + ")\n\tEndPosition XY = (\n\t\t";
		for(int i = 0; i < EndPosition_XY_Graph.size(); i++){
			data = data + "[" + EndPosition_XY_Graph.get(i) + "],";
		}
		data = data + ")\n\tStartTime Drag = (\n\t\t";
		for(int i =0;i<StartTime_Drag_Graph.size();i++){
			data = data + "[" + StartTime_Drag_Graph.get(i) + "],";
		}
		data = data + ")\n\tEndTime Drag = (\n\t\t";
		for(int i = 0; i<EndTime_Drag_Graph.size(); i++){
			data = data + "[" + EndTime_Drag_Graph.get(i) + "],";
		}
		data = data + ")\n============================================================================\n";
	
		// +-- SeekBar --+ //
		data = data + "SeekBar : \n\tDragXY = (\n\t\t";
		int size3 = DragXY_SeekBar.size();
		for(int i = 0; i < size3; i++){
			data = data + "[" + DragXY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartTime Touch = (\n\t\t";
		for(int i = 0; i < StartTime_Touch_SeekBar.size(); i++){
			data = data + "[" + StartTime_Touch_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartPosition XY = (\n\t\t";
		for(int i = 0; i < StartPosition_XY_SeekBar.size(); i++){
			data = data + "[" + StartPosition_XY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tEndPosition XY = (\n\t\t";
		for(int i = 0; i < EndPosition_XY_SeekBar.size(); i++){
			data = data + "[" + EndPosition_XY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartTime Drag = (\n\t\t";
		for(int i =0;i<StartTime_Drag_SeekBar.size();i++){
			data = data + "[" + StartTime_Drag_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tEndTime Drag = (\n\t\t";
		for(int i = 0; i<EndTime_Drag_SeekBar.size(); i++){
			data = data + "[" + EndTime_Drag_SeekBar.get(i) + "],";
		}
		data = data + ")\n============================================================================\n";
	
		// +-- SinCos --+ //
		data = data + "Click Sin Cos : \n\tClick SinCos = [\n\t\t";
		int size4 = Click_SinCos.size();
		for(int i = 0; i < size4; i++){
			data = data + "[" + Click_SinCos.get(i) + "],";
		}
		data = data + "\n\tTime Click Button SinCos = [\n\t\t";
		for(int i = 0; i < size4; i++){
			data = data + "[" + StartTime_Click_BtnSinCos.get(i) + "],";
		}
		data = data + "\n\tValue XY = [\n\t\t";
		for(int i = 0; i < size4; i++){
			data = data + "[" + Value_XY_BtnSinCos.get(i) + "],";
		}
		data = data + "\n============================================================================\n";
				
		// +-- Sureness --+ //
		data = data + "Sureness : \n\t";
		int size5 = Time_Sureness.size();
		for(int i = 0; i < size5; i++){
			data = data + "Click Button = [" + Value_Sureness.get(i) + " , " + Time_Sureness.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- ScreenShot --+ //
		data = data + "ScreenShot : \n\t";
		int size6 = Time_ScreenShot.size();
		for(int i = 0; i < size6; i++){
			data = data + "Click Button = [" + NO_ScreenShot.get(i) + " , " + Time_ScreenShot.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- Player --+ //
		data = data + "Angle Player : \n\t";
		int size7 = Time_OkBtn.size();
		for(int i = 0; i < size7; i++){
			data = data + "Click Button = [" + Angle_Player.get(i) + " , " + Angle_Discrepancy.get(i) + " , " + Time_OkBtn.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		return data;
	}
	
	
	// ==================================================================================== //
	public static void SaveDataTofile(){
		String filename = "Game_" + TaskNO +".txt";
		String path = "/Mattrigle/" + PlayerName + "/" + Angle + "/" + DateTime_StartTask;
		
		String root = Environment.getExternalStorageDirectory().toString();
		
		File myFile = new File(root + path);
		String data = "";
		try {
			data = jsonFormat();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {    		
    		myFile.mkdirs();
    		
    		File newFile = new File(myFile, filename);
    		newFile.createNewFile();
    		FileOutputStream fos = new FileOutputStream(newFile);
    		OutputStreamWriter osw = new OutputStreamWriter(fos);
    		osw.write(data);
    		osw.close();
		} catch (IOException e) {
			Log.e("Emoji", "File write failed: "+e.toString());
		}
		
	}
	// ==================================================================================== //
	
	public static void clearArray(){
		ID_SinCos.clear();
		Time_ClickSinCos.clear();
		
		DragXY_Graph.clear();
		StartTime_Touch_Graph.clear();
		StartPosition_XY_Graph.clear();
		StartTime_Drag_Graph.clear();
		EndPosition_XY_Graph.clear();
		EndTime_Drag_Graph.clear();
		
		DragXY_SeekBar.clear();
		StartTime_Touch_SeekBar.clear();
		StartPosition_XY_SeekBar.clear();
		StartTime_Drag_SeekBar.clear();
		EndPosition_XY_SeekBar.clear();
		EndTime_Drag_SeekBar.clear();
		
		Click_SinCos.clear();
		StartTime_Click_BtnSinCos.clear();
		Value_XY_BtnSinCos.clear();
		
		Time_Sureness.clear();
		Value_Sureness.clear();
		
		Time_ScreenShot.clear();
		NO_ScreenShot.clear();
		
		Time_OkBtn.clear();
		Angle_Player.clear();	
		Angle_Discrepancy.clear();
		
		CosMinus_click.clear();
		CosMinus_time.clear();
		CosMinus_value.clear();
		
		CosPlus_click.clear();
		CosPlus_time.clear();
		CosPlus_value.clear();
		
		CosSpin_click.clear();
		CosSpin_time.clear();
		CosSpin_value.clear();
		
		SinMinus_click.clear();
		SinMinus_time.clear();
		SinMinus_value.clear();
		
		SinPlus_click.clear();
		SinPlus_time.clear();
		SinPlus_value.clear();
		
		SinSpin_click.clear();
		SinSpin_time.clear();
		SinSpin_value.clear();
		
	}
	
	public static String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String currentDateandTime = sdf.format(new Date());
		return currentDateandTime;
	}
	
	public static String formatData(long data){
		Date date = new Date(data);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentDateandTime = sdf.format(date);
		return currentDateandTime;
	}
}





